package com.cqs.leetcode;

/**
 * 使用暴力计算配合缓存会超时
 * <p>
 * 缓存配合sumRange(i,j)=sumRange(i,j-1)+nums[j]栈溢出
 *
 * Created by cqs on 2018/4/8.
 */
public class NumArray {

    private int[] sums;

    public NumArray(int[] nums) {
        sums = new int[nums.length];
        if (nums.length != 0) {
            sums[0] = nums[0];
            //初始化
            for (int i = 1; i < nums.length; i++) {
                sums[i] = sums[i - 1] + nums[i];
            }
        }
    }

    public int sumRange(int i, int j) {
        if (i == 0) return sums[j];
        return sums[j] - sums[i - 1];
    }
}
